package com.youngwang.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Properties;

/**
 * Spring Boot 获取资源配置文件数据
 *
 * @author youngwang
 *         Date: 2016/9/28
 */
@Component
public class SystemConfigUtils {
    private static Properties props;

    public SystemConfigUtils() {
    }

    public static String getProperty(String key) {
        if (null == props) {
            props = getProperties();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if (null == props) {
            props = getProperties();
        }
        return props.getProperty(key, defaultValue);
    }

    public static Properties getProperties() {
        try {
            Resource resource = new ClassPathResource("/application.properties");
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }
}
